﻿
using CSharpLibs.Annotations;

#pragma warning disable 0169

namespace System.IO
{
	[Builtin]
	public class FileStream : Stream, IDisposable
	{
		private FileMode fmode;
		private FileAccess faccess;
		private FileShare fshare;

		private string fpath;
		private int state;

		public FileStream(string path, FileMode mode)
			: base()
		{ 
			this.fmode = mode;

			if(mode == FileMode.Append)
				this.faccess = FileAccess.Write;
			else
				this.faccess = FileAccess.ReadWrite;

			this.fshare = FileShare.Read;

			this.fpath = path;
			this.InitialiseFileStream(path);
		}

		public FileStream(string path, FileMode mode, FileAccess access, FileShare share)
			: base()
		{
			this.fmode = mode;
			this.faccess = access;
			this.fshare = share;
			this.fpath = path;
			this.InitialiseFileStream(path);
		}

		[BuiltinMethod, BuiltinSemantics("filestream_init")]
		[UsedModStatics("System.ExternalState.fsstate")]
		private void InitialiseFileStream(string path)
		{ ;}

		[BuiltinMethod, BuiltinSemantics("filestream_close")]
		[UsedModStatics("System.ExternalState.fsstate")]
		public override void Close()
		{ ;}

		[BuiltinMethod, BuiltinSemantics("filestream_read")]
		[UsedModStatics("System.ExternalState.fsstate")]
		public override int Read(byte[] array, int offset, int count)
		{ return -1; }

		public override int ReadByte()
		{
			byte[] buffer = new byte[1];
			if(this.Read(buffer, 0, 1) == 0)
				return -1;

			return buffer[0];
		}

		[BuiltinMethod, BuiltinSemantics("filestream_seek")]
		[UsedModStatics("System.ExternalState.fsstate")]
		public override long Seek(long offset, SeekOrigin origin)
		{ return (long)-1; }

		[BuiltinMethod, BuiltinSemantics("filestream_write")]
		[UsedModStatics("System.ExternalState.fsstate")]
		public override void Write(byte[] buffer, int offset, int count)
		{ ;}

		public override void WriteByte(byte value)
		{
			byte[] buffer = new byte[1];
			buffer[0] = value;
			this.Write(buffer, 0, 1);
		}

		public override long Length
		{
			[BuiltinMethod, BuiltinSemantics("filestream_length")]
			[UsedModStatics("System.ExternalState.fsstate")]
			get { return (long)-1; }
		}
	}
}

#pragma warning restore 0169
